﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RayDen.Library.Data.Xml;
using RayDen.Library.Entity.Frames;

namespace UITools
{
    public partial class FramesourceForm : Form
    {
        public string FrameSource { get; set; }

        private string[] TypeWords = new[] {"RgbSpectrum", "Vector"};

        public FramesourceForm()
        {
            InitializeComponent();
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            SetText();
        }

        private void SetText()
        {
            this.txtSource.Text = this.FrameSource;
            //ColorizeText(TypeWords[0], Color.Red);
        }

        private void ColorizeText(string text, Color col)
        {
            bool done = false;
            int index = 0;
            while (!done)
            {
                index = FrameSource.IndexOf(text, index);
                this.txtSource.SelectionColor = col;
                this.txtSource.Select(index, text.Length);
                done = index == -1;
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            this.FrameSource = this.txtSource.Text;
            (this.Owner as AppForm).SetupFrame(this.FrameSource);
            this.Close();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                using (var fs = new FileStream(saveFileDialog1.FileName, FileMode.OpenOrCreate, FileAccess.Write))
                    using (var sw = new StreamWriter(fs))
                {
                    sw.Write(this.FrameSource);
                }
            }
            this.Text = saveFileDialog1.FileName+" Saved ";
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            try
            {
                var Frame = (FrameDescription) SerializationService.Deserialize(FrameSource, typeof (FrameDescription));
                this.Text = "Frame builded successfully";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
    }
}
